# encoding: utf-8 
import urllib2
import threading
import time
from PyQt4.QtCore import pyqtSignature, QDateTime
from PyQt4.QtGui import QMainWindow, QApplication
from Ui_XiaoMiSenQi import Ui_MainWindow
import login
import commonFunc

class XiaoMiSenQi(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.loginABtn.opener=None
        self.loginBBtn.opener=None
        self.loginCBtn.opener=None
        self.loginDBtn.opener=None
        
        self.running=False
        self.configUrl='conf.ini'
        self.config={}
        try:
            self.config=commonFunc.loadArchive(self.configUrl)
            self.loginAddrEdit.setText(self.config['loginAddr'])
            self.successAddrEdit.setText(self.config['successAddr'])
            self.goodAddrEdit.setText(self.config['goodAddr'])
            self.goodParaEdit.setText(self.config['goodPara'])
            self.timeEdit.setDateTime(QDateTime.fromString(self.config['timePurchase'],'yyyy-MM-dd hh:mm:ss'))
        except:
            info=sys.exc_info()
            print '%s:%s'%(info[0], info[1])
            
    @pyqtSignature("")
    def on_goBtn_clicked(self):
        self.running= not self.running
        if self.running==True:
            thr=threading.Thread(target=self.readyForPurchase, args=())
            thr.setDaemon(True)
            thr.start()
            self.goBtn.setText(u'停止')
        else:
            self.goBtn.setText(u'开抢')
            self.statusP.setText(u'')

    def readyForPurchase(self):
        readyTime=commonFunc.str2Unixtime(self.config['timePurchase'])
        while self.running==True:
            if time.time()>readyTime:
                self.postDingDan(1,2,2)
                self.statusP.setText(u'正在拼命，急个毛线')
            else:
                self.statusP.setText(u'时间未到，蓄势待发')
            time.sleep(0.5)
    def postDingDan(self,url,data,opener):
        print 123
        '''
        headers={
            
        }
        request=urllib2.Request(url,data,headers)
        response=opener.open(request)
        res =response.read()
        print res
        return res
        '''
        
    def loginAccount(self, btn,status, loginUrl, successUrl, goodUrl):
        login.login(btn,status, loginUrl, successUrl, goodUrl).show()
    @pyqtSignature("")
    def on_loginABtn_clicked(self):
        if self.loginABtn.opener==None:
            self.loginAccount(self.loginABtn,self.statusA,self.config['loginAddr'], self.config['successAddr'], self.config['goodAddr'])
    @pyqtSignature("")
    def on_loginBBtn_clicked(self):
       if self.loginBBtn.opener==None:
            self.loginAccount(self.loginBBtn,self.statusB,self.config['loginAddr'], self.config['successAddr'], self.config['goodAddr'])
    @pyqtSignature("")
    def on_loginCBtn_clicked(self):
        if self.loginCBtn.opener==None:
            self.loginAccount(self.loginCBtn,self.statusC,self.config['loginAddr'], self.config['successAddr'], self.config['goodAddr'])
    @pyqtSignature("")
    def on_loginDBtn_clicked(self):
        if self.loginDBtn.opener==None:
            self.loginAccount(self.loginDBtn,self.statusD,self.config['loginAddr'], self.config['successAddr'], self.config['goodAddr'])
    @pyqtSignature("QString")
    def on_loginAddrEdit_textChanged(self, p0):
        self.config['loginAddr']=str(p0)
        commonFunc.saveArchive(self.configUrl, self.config)
    @pyqtSignature("QString")
    def on_goodAddrEdit_textChanged(self, p0):
        self.config['goodAddr']=str(p0)
        commonFunc.saveArchive(self.configUrl, self.config)
    @pyqtSignature("QString")
    def on_goodParaEdit_textChanged(self, p0):
        self.config['goodPara']=str(p0)
        commonFunc.saveArchive(self.configUrl, self.config)
    @pyqtSignature("QString")
    def on_successAddrEdit_textChanged(self, p0):
        self.config['successAddr']=str(p0)
        commonFunc.saveArchive(self.configUrl, self.config)
    @pyqtSignature("QTime")
    def on_timeEdit_timeChanged(self, date):
        self.config['timePurchase']=str(self.timeEdit.dateTime().toString('yyyy-MM-dd hh:mm:ss'))
        commonFunc.saveArchive(self.configUrl, self.config)
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    senqi = XiaoMiSenQi()
    senqi.show()
    sys.exit(app.exec_())
